package cn.tedu.medicalmanagementsystem.user.service.impl;
import cn.tedu.medicalmanagementsystem.base.response.JsonResult;
import cn.tedu.medicalmanagementsystem.base.response.StatusCode;
import cn.tedu.medicalmanagementsystem.user.mapper.UserFavoriteMapper;
import cn.tedu.medicalmanagementsystem.user.pojo.dto.UserFavoriteAddParam;
import cn.tedu.medicalmanagementsystem.user.pojo.entity.UserFavorite;
import cn.tedu.medicalmanagementsystem.user.pojo.vo.UserFavoriteVO;
import cn.tedu.medicalmanagementsystem.user.service.UserFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


    @Service
    public class UserFavoriteServiceImpl implements UserFavoriteService {

        @Autowired
        private UserFavoriteMapper favoriteMapper;

        @Override
        public JsonResult addFavorite(UserFavoriteAddParam param, Long userId) {
            if (favoriteMapper.exists(userId, param.getEscortId()) > 0) {
                return JsonResult.fail(StatusCode.OPERATION_FAILED, "该陪诊员已收藏");
            }

            UserFavorite favorite = new UserFavorite();
            favorite.setUserId(userId);
            favorite.setEscortId(param.getEscortId());
            favorite.setCreateTime(LocalDateTime.now());

            int rows = favoriteMapper.insert(favorite);
            if (rows != 1) {
                return JsonResult.fail(StatusCode.OPERATION_FAILED, "收藏失败");
            }
            return JsonResult.ok("收藏成功");
        }

        @Override
        public JsonResult removeFavorite(Long escortId, Long userId) {
            int rows = favoriteMapper.delete(userId, escortId);
            if (rows != 1) {
                return JsonResult.fail(StatusCode.OPERATION_FAILED, "取消收藏失败");
            }
            return JsonResult.ok("取消收藏成功");
        }

        @Override
        public JsonResult<List<UserFavoriteVO>> listFavorites(Long userId) {
            List<UserFavoriteVO> favorites = favoriteMapper.listByUserId(userId);
            return JsonResult.ok(favorites);
        }

        @Override
        public JsonResult<Boolean> checkFavorite(Long escortId, Long userId) {
            boolean isFavorite = favoriteMapper.exists(userId, escortId) > 0;
            return JsonResult.ok(isFavorite);
        }
    }


